package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.MD5Utils;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.StringUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.CustomerBiz;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.CustomerCopy;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2020-06-02 13:34:13
 */
@Repository
public class CustomerBizImpl extends BaseDao implements CustomerBiz {

  @Override
  public JsonResult list(PageResult2<T> page, Customer customer, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
    StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name,c.abbreviation,c.profile_picture,c.pinyin");
    sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.settle");
    sql.append(",c.road_transport_permit,c.agreement,c.agreement_start,c.agreement_end");
    sql.append(",c.email,c.contacts,c.contacts_phone,c.address,c.message,c.gzh_send");
    sql.append(",c.salesmanId,c.salesman,salesman.phone salesmanPhone,c.operaterId,c.operater,operater.phone operaterPhone,c.invoice_state");
    sql.append(",c.type,c.state,c.order_num,c.offer_limit_num,c.create_time,c.create_user");
    sql.append(",c.update_time,c.update_user");
    sql.append(",sognz.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_customer c ");
    sql.append(" left join sys_user suser on suser.user_id=c.create_user ");
    sql.append(" left join sys_user salesman on salesman.user_id=c.salesmanId ");
    sql.append(" left join sys_user operater on operater.user_id=c.operaterId ");
    sql.append(" left join sys_organization sognz on sognz.organization_id=c.organization_id ");
    sql.append(" where c.deleted=0 and sognz.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and c.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(customer)) {
      if (Utils.isNotEmpty(customer.getType())) {
        sql.append(" and instr(c.type,:type)>0 ");
        map.put("type", customer.getType());
      }
      if (Utils.isNotEmpty(customer.getState())) {
        sql.append(" and instr(c.state,:state)>0 ");
        map.put("state", customer.getState());
      }
      if (Utils.isNotEmpty(customer.getSalesman())) {
        sql.append(" and instr(c.salesman,:salesman)>0 ");
        map.put("salesman", customer.getSalesman());
      }
      if (Utils.isNotEmpty(customer.getOperater())) {
        sql.append(" and instr(c.operater,:operater)>0 ");
        map.put("operater", customer.getOperater());
      }
      if (Utils.isNotEmpty(customer.getSettle())) {
        sql.append(" and instr(c.settle,:settle)>0 ");
        map.put("settle", customer.getSettle());
      }
      if (Utils.isNotEmpty(customer.getName())) {
        sql.append(" and (instr(c.name,:name)>0 or instr(c.pinyin,:name)>0) ");
        map.put("name", customer.getName().toLowerCase());
      }
      if (Utils.isNotEmpty(customer.getSort()) && Utils.isNotEmpty(customer.getOrder())) {
        sql.append(" order by " + customer.getSort() + " " + customer.getOrder());
      } else {
        sql.append(" order by c.create_time desc");
      }
    }
    JsonResult jr2 = jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    List<Customer> customerList = (List<Customer>) jr2.getData();
    customerList.forEach(cus -> {
      if (!StringUtil.isBlank(cus.getSalesmanPhone(), cus.getOperaterPhone())) {
        cus.setSalesmanPhone(MD5Utils.convertMD5(cus.getSalesmanPhone()));
        cus.setOperaterPhone(MD5Utils.convertMD5(cus.getOperaterPhone()));
      }
    });
    return jr2;
  }

  @Override
  public List<Customer> list(Customer customer, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
    StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name,c.abbreviation,c.profile_picture,c.pinyin");
    sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.settle");
    sql.append(",c.road_transport_permit,c.agreement,c.agreement_start,c.agreement_end");
    sql.append(",c.email,c.contacts,c.contacts_phone,c.address,c.message,c.gzh_send");
    sql.append(",c.salesmanId,c.salesman,salesman.phone salesmanPhone,c.operaterId,c.operater,operater.phone operaterPhone,c.invoice_state");
    sql.append(",c.type,c.state,c.order_num,c.offer_limit_num,c.create_time,c.create_user");
    sql.append(",c.update_time,c.update_user");
    sql.append(",sognz.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_customer c ");
    sql.append(" left join sys_user suser on suser.user_id=c.create_user ");
    sql.append(" left join sys_user salesman on salesman.user_id=c.salesmanId ");
    sql.append(" left join sys_user operater on operater.user_id=c.operaterId ");
    sql.append(" left join sys_organization sognz on sognz.organization_id=c.organization_id ");
    sql.append(" where c.deleted=0 and sognz.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and c.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(customer)) {
      if (Utils.isNotEmpty(customer.getType())) {
        sql.append(" and instr(c.type,:type)>0 ");
        map.put("type", customer.getType());
      }
      if (Utils.isNotEmpty(customer.getState())) {
        sql.append(" and instr(c.state,:state)>0 ");
        map.put("state", customer.getState());
      }
      if (Utils.isNotEmpty(customer.getSalesman())) {
        sql.append(" and instr(c.salesman,:salesman)>0 ");
        map.put("salesman", customer.getSalesman());
      }
      if (Utils.isNotEmpty(customer.getOperater())) {
        sql.append(" and instr(c.operater,:operater)>0 ");
        map.put("operater", customer.getOperater());
      }
      if (Utils.isNotEmpty(customer.getSettle())) {
        sql.append(" and instr(c.settle,:settle)>0 ");
        map.put("settle", customer.getSettle());
      }
      if (Utils.isNotEmpty(customer.getName())) {
        sql.append(" and (instr(c.name,:name)>0 or instr(c.pinyin,:name)>0) ");
        map.put("name", customer.getName().toLowerCase());
      }
      if (Utils.isNotEmpty(customer.getSort()) && Utils.isNotEmpty(customer.getOrder())) {
        sql.append(" order by " + customer.getSort() + " " + customer.getOrder());
      } else {
        sql.append(" order by c.create_time desc");
      }
    }
    getMapParameter(sql, map);
    List<Customer> customerList = jdbcTemplate.query(sql.toString(), map, rm);
    customerList.forEach(cus -> {
      if (!StringUtil.isBlank(cus.getSalesmanPhone(), cus.getOperaterPhone())) {
        cus.setSalesmanPhone(MD5Utils.convertMD5(cus.getSalesmanPhone()));
        cus.setOperaterPhone(MD5Utils.convertMD5(cus.getOperaterPhone()));
      }
    });
    return customerList;
  }

  @Override
  public Customer getById(Customer customer) {
    RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name,c.abbreviation,c.profile_picture,c.pinyin");
    sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.settle");
    sql.append(",c.road_transport_permit,c.agreement,c.agreement_start,c.agreement_end");
    sql.append(",c.email,c.contacts,c.contacts_phone,c.address,c.message,c.gzh_send");
    sql.append(",c.salesmanId,c.salesman,salesman.phone salesmanPhone,c.operaterId,c.operater,operater.phone operaterPhone,c.invoice_state");
    sql.append(",c.type,c.state,c.order_num,c.offer_limit_num,c.create_time,c.create_user");
    sql.append(",c.update_time,c.update_user");
    sql.append(",sognz.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_customer c ");
    sql.append(" left join sys_user suser on suser.user_id=c.create_user ");
    sql.append(" left join sys_user salesman on salesman.user_id=c.salesmanId ");
    sql.append(" left join sys_user operater on operater.user_id=c.operaterId ");
    sql.append(" left join sys_organization sognz on sognz.organization_id=c.organization_id ");
    sql.append(" where c.deleted=0 and sognz.deleted=0 and c.id=:id ");
    map.put("id", customer.getId());
    getMapParameter(sql, map);
    Customer cus = jdbcTemplate.queryForObject(sql.toString(), map, rm);
    try {
      cus.setSalesmanPhone(MD5Utils.convertMD5(cus.getSalesmanPhone()));
      cus.setOperaterPhone(MD5Utils.convertMD5(cus.getOperaterPhone()));
    } catch (Exception e) {
    }
    return cus;
  }

  @Override
  public List<Customer> getByName(Customer customer) {
    RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name,c.abbreviation,c.profile_picture,c.pinyin");
    sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.settle");
    sql.append(",c.road_transport_permit,c.agreement,c.agreement_start,c.agreement_end");
    sql.append(",c.email,c.contacts,c.contacts_phone,c.address,c.message,c.gzh_send");
    sql.append(",c.salesmanId,c.salesman,salesman.phone salesmanPhone,c.operaterId,c.operater,operater.phone operaterPhone,c.invoice_state");
    sql.append(",c.type,c.state,c.order_num,c.offer_limit_num,c.create_time,c.create_user");
    sql.append(",c.update_time,c.update_user");
    sql.append(",sognz.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_customer c ");
    sql.append(" left join sys_user suser on suser.user_id=c.create_user ");
    sql.append(" left join sys_user salesman on salesman.user_id=c.salesmanId ");
    sql.append(" left join sys_user operater on operater.user_id=c.operaterId ");
    sql.append(" left join sys_organization sognz on sognz.organization_id=c.organization_id ");
    sql.append(" where c.deleted=0 and sognz.deleted=0 and c.name=:name ");
    map.put("name", customer.getName());
    getMapParameter(sql, map);
    List<Customer> customerList = jdbcTemplate.query(sql.toString(), map, rm);
    customerList.forEach(cus -> {
      if (!StringUtil.isBlank(cus.getSalesmanPhone(), cus.getOperaterPhone())) {
        cus.setSalesmanPhone(MD5Utils.convertMD5(cus.getSalesmanPhone()));
        cus.setOperaterPhone(MD5Utils.convertMD5(cus.getOperaterPhone()));
      }
    });
    return customerList;
  }

  @Override
  public Customer getIdByName(Customer customer) {
    RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name,c.abbreviation,c.profile_picture,c.pinyin");
    sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.settle");
    sql.append(",c.road_transport_permit,c.agreement,c.agreement_start,c.agreement_end");
    sql.append(",c.email,c.contacts,c.contacts_phone,c.address,c.message,c.gzh_send");
    sql.append(",c.salesmanId,c.salesman,salesman.phone salesmanPhone,c.operaterId,c.operater,operater.phone operaterPhone,c.invoice_state");
    sql.append(",c.type,c.state,c.order_num,c.offer_limit_num,c.create_time,c.create_user");
    sql.append(",c.update_time,c.update_user");
    sql.append(",sognz.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_customer c ");
    sql.append(" left join sys_user suser on suser.user_id=c.create_user ");
    sql.append(" left join sys_user salesman on salesman.user_id=c.salesmanId ");
    sql.append(" left join sys_user operater on operater.user_id=c.operaterId ");
    sql.append(" left join sys_organization sognz on sognz.organization_id=c.organization_id ");
    sql.append(" where c.deleted=0 and sognz.deleted=0 and c.name=:name ");
    map.put("name", customer.getName());
    sql.append(" and c.type=:type ");
    map.put("type", customer.getType());
    getMapParameter(sql, map);
    Customer cus = jdbcTemplate.queryForObject(sql.toString(), map, rm);
    cus.setSalesmanPhone(MD5Utils.convertMD5(cus.getSalesmanPhone()));
    cus.setOperaterPhone(MD5Utils.convertMD5(cus.getOperaterPhone()));
    return cus;
  }

  @Override
  public boolean save(Customer customer, User loginUser) {
    customer.setPinyin(PinyinUtils.ToFirstChar(customer.getName()));
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into chf_customer (");
    sql.append("organization_id,name,abbreviation,profile_picture,pinyin,admin_user,code,social_code,business_license");
    sql.append(",road_transport_permit,agreement");
    sql.append(",email,contacts,contacts_phone,address,message,gzh_send");
    sql.append(",salesmanId,salesman,operaterId,operater,invoice_state");
    sql.append(",settle,type,state,order_num,offer_limit_num");
    sql.append(",create_user,create_time) values ( ");
    sql.append(":organization_id,:name,:abbreviation,:profile_picture,:pinyin,:code,:admin_user,:social_code,:business_license");
    sql.append(",:road_transport_permit,:agreement");
    sql.append(",:email,:contacts,:contacts_phone,:address,1,0");
    sql.append(",:salesmanId,:salesman,:operaterId,:operater,:invoice_state");
    sql.append(",:settle,:type,:state,:order_num,:offer_limit_num");
    sql.append(",:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("name", customer.getName());
    map.put("abbreviation", customer.getAbbreviation());
    map.put("profile_picture", customer.getProfilePicture());
    map.put("pinyin", customer.getPinyin());
    map.put("admin_user", customer.getAdminUser());
    map.put("code", customer.getCode());
    map.put("social_code", customer.getSocialCode());
    map.put("business_license", customer.getBusinessLicense());
    map.put("road_transport_permit", customer.getRoadTransportPermit());
    map.put("agreement", customer.getAgreement());
    map.put("email", customer.getEmail());
    map.put("contacts", customer.getContacts());
    map.put("contacts_phone", customer.getContactsPhone());
    map.put("address", customer.getAddress());
    map.put("salesmanId", customer.getSalesmanId());
    map.put("salesman", customer.getSalesman());
    map.put("operaterId", customer.getOperaterId());
    map.put("operater", customer.getOperater());
    map.put("settle", customer.getSettle());
    map.put("invoice_state", customer.getInvoiceState());
    map.put("type", customer.getType());
    map.put("state", customer.getState());
    map.put("order_num", customer.getOrderNum() == null ? null : customer.getOrderNum() + "");
    map.put("offer_limit_num", Utils.isNotEmpty(customer.getOfferLimitNum()) ? customer.getOfferLimitNum() : UserConstant.OFFER_LIMIT_NUM);
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public Integer ApiSave(CustomerCopy customer, User loginUser) {
    customer.setPinyin(PinyinUtils.ToFirstChar(customer.getName()));
    GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
    KeyHolder keyHolder = new GeneratedKeyHolder();
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into chf_customer (");
    sql.append("organization_id,name,abbreviation,profile_picture,pinyin,admin_user,code,social_code,business_license");
    sql.append(",road_transport_permit,agreement,agreement_start,agreement_end");
    sql.append(",email,contacts,contacts_phone,address,message,gzh_send");
    sql.append(",salesmanId,salesman,operaterId,operater,invoice_state");
    sql.append(",settle,type,state,order_num,offer_limit_num");
    sql.append(",create_user, create_time) values ( ");
    sql.append(":organization_id,:name,:abbreviation,:profile_picture,:pinyin,:admin_user,:code,:social_code,:business_license");
    sql.append(",:road_transport_permit,:agreement,:agreement_start,:agreement_end");
    sql.append(",:email,:contacts,:contacts_phone,:address,1,0");
    sql.append(",:salesmanId,:salesman,:operaterId,:operater,:invoice_state");
    sql.append(",:settle,:type,:state,:order_num,:offer_limit_num");
    sql.append(",:create_user, now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("name", customer.getName());
    map.put("abbreviation", customer.getAbbreviation());
    map.put("profile_picture", customer.getProfilePicture());
    map.put("pinyin", customer.getPinyin());
    map.put("admin_user", customer.getAdminUser());
    map.put("code", customer.getCode());
    map.put("social_code", customer.getSocialCode());
    map.put("business_license", customer.getBusinessLicense());
    map.put("road_transport_permit", customer.getRoadTransportPermit());
    map.put("agreement", customer.getAgreement());
    map.put("agreement_start", customer.getAgreementStart());
    map.put("agreement_end", customer.getAgreementEnd());
    map.put("email", customer.getEmail());
    map.put("contacts", customer.getContacts());
    map.put("contacts_phone", customer.getContactsPhone());
    map.put("address", customer.getAddress());
    map.put("salesmanId", customer.getSalesmanId());
    map.put("salesman", customer.getSalesman());
    map.put("operaterId", customer.getOperaterId());
    map.put("operater", customer.getOperater());
    map.put("settle", customer.getSettle());
    map.put("type", customer.getType());
    map.put("invoice_state", customer.getInvoiceState());
    map.put("state", customer.getState());
    map.put("order_num", customer.getOrderNum() == null ? null : customer.getOrderNum() + "");
    map.put("offer_limit_num", Utils.isNotEmpty(customer.getOfferLimitNum()) ? customer.getOfferLimitNum() : UserConstant.OFFER_LIMIT_NUM);
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);


//		System.err.println("受到影响的行数=" + retVal + ",
    int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
      @Override
      public boolean hasValue(String paramName) {
        return map.containsKey(paramName);
      }

      @Override
      public Object getValue(String paramName) throws IllegalArgumentException {
        return map.get(paramName);
      }
    }, generatedKeyHolder);

    Integer id = generatedKeyHolder.getKey().intValue();
    return id;
  }

  @Override
  public boolean update(Customer customer, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_customer set ");
    sql.append("code=:code, abbreviation=:abbreviation, profile_picture=:profile_picture ");
    sql.append(",settle=:settle,email=:email ");
    if (Utils.isNotEmpty(customer.getAgreementStart())) {
      sql.append(",agreement_start=:agreement_start ");
    }
    if (Utils.isNotEmpty(customer.getAgreementStart())) {
      sql.append(",agreement_end=:agreement_end ");
    }
    sql.append(",contacts=:contacts,contacts_phone=:contacts_phone,address=:address");
    sql.append(",salesmanId=:salesmanId,salesman=:salesman,operaterId=:operaterId,operater=:operater");
    sql.append(",order_num=:order_num,offer_limit_num=:offer_limit_num,update_user=:update_user where id=:id ");
//        map.put("name", customer.getName());
    map.put("abbreviation", customer.getAbbreviation());
    map.put("code", customer.getCode());
    map.put("profile_picture", customer.getProfilePicture());
//        map.put("social_code", customer.getSocialCode());
//        map.put("business_license", customer.getBusinessLicense());
    map.put("settle", customer.getSettle());
    map.put("email", customer.getEmail());

    if (Utils.isNotEmpty(customer.getAgreementStart())) {
      map.put("agreement_start", customer.getAgreementStart());
    }
    if (Utils.isNotEmpty(customer.getAgreementStart())) {
      map.put("agreement_end", customer.getAgreementEnd());
    }
    map.put("contacts", customer.getContacts());
    map.put("contacts_phone", customer.getContactsPhone());
    map.put("address", customer.getAddress());
    map.put("salesmanId", customer.getSalesmanId());
    map.put("salesman", customer.getSalesman());
    map.put("operaterId", customer.getOperaterId());
    map.put("operater", customer.getOperater());
//        map.put("state", customer.getState());
    map.put("order_num", customer.getOrderNum() == null ? null : customer.getOrderNum() + "");
    map.put("offer_limit_num", Utils.isNotEmpty(customer.getOfferLimitNum()) ? customer.getOfferLimitNum() : UserConstant.OFFER_LIMIT_NUM);
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", customer.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean update2(Customer customer, User loginUser) {
    customer.setPinyin(PinyinUtils.ToFirstChar(customer.getName()));
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_customer set ");
    sql.append("pinyin=:pinyin ");
    sql.append(" where id=:id ");
    map.put("pinyin", customer.getPinyin());
    map.put("id", customer.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean updateInvoiceState(Customer customer, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_customer set ");
    sql.append("invoice_state=:invoice_state ");
    sql.append(" where id=:id ");
    map.put("id", customer.getId());
    map.put("invoice_state", customer.getInvoiceState());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean updateMessage(Customer customer, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_customer set ");
    sql.append("message=:message ");
    sql.append(" where id=:id ");
    map.put("message", customer.getMessage());
    map.put("id", customer.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean updateGzhSend(Customer customer, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_customer set ");
    sql.append("gzh_send=:gzh_send ");
    sql.append(" where id=:id ");
    map.put("gzh_send", customer.getGzhSend());
    map.put("id", customer.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean examine(Customer customer, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_customer set ");
    sql.append("state=:state ");
    sql.append(",update_user=:update_user where id=:id ");
    map.put("state", customer.getState());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", customer.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean examineFail(Customer customer, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_customer set ");
    sql.append("state=:state ");
    sql.append(",update_user=:update_user where id=:id ");
    map.put("state", customer.getState());
    map.put("order_num", customer.getOrderNum() == null ? null : customer.getOrderNum() + "");
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", customer.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean remove(Customer customer, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (customer.getId().length() > 0) {
      for (String ID : customer.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update chf_customer set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }
}
